﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Threading;
using Microsoft.Xna.Framework.Graphics;
namespace sand_game_core
{
    public static class world
    {
        public static sand_game_core.newParticle[] worldParticleLayer;
        public static uint width, height, arraywidth, arrayheight, i, j, k, l, m, n, Width, Height, factX, factY;
        public static Rectangle rect;
        public static uint edited;
        static newParticle updated, swapped, target;
        private static uint[] backRender;
        private static Color tempColor;
        private static Texture2D Render;
        public static void initialize(Rectangle Window, Microsoft.Xna.Framework.Graphics.GraphicsDevice dev)
        {

            NewParticle.init();
            edited = 0;
            arraywidth = 10000;
            arrayheight = 10000;
            width = (uint)Window.Width;
            height = (uint)Window.Height;
            rect = new Rectangle(0, 0, (int)width, (int)height);
            NewParticle.myImage = new Microsoft.Xna.Framework.Graphics.Texture2D(dev, 1, 1);
            NewParticle.myImage.SetData<byte>(new byte[4] { 0xff, 0xff, 0xff, 0xff });
        }
        public static void update()
        {
            while (view.multiply == 0)
            {
            }

            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            uint Width = (uint)width / (uint)view.multiply + view.x;
            uint Height = (uint)height / (uint)view.multiply + view.y;
            while (true)
            {
                for (k = view.x - 300; k < view.x + width + 300 || k > arraywidth; k++)
                {
                    for (l = view.y - 300; l < view.y + height + 300 || l > arrayheight; l++)
                    {

                        if (k - 1 < arraywidth - 2 && l - 1 < arrayheight - 2)
                        {
                            NewParticle.Light.Invoke(k, l);
                            if (worldParticleLayer[k * arraywidth + l].myType != 0)
                                NewParticle.toUpdate.Invoke(k, l);
                        }
                        /*
                        if (k < arraywidth && l < arrayheight && worldParticleLayer[i*arraywidth+j] != null)
                        {
                        if (worldParticleLayer[i*arraywidth+j].toTransition != null)
                            worldParticleLayer[i*arraywidth+j] = worldParticleLayer[i*arraywidth+j].toTransition;
                            if (worldParticleLayer[i*arraywidth+j].toTransition != null)
                                worldParticleLayer[i*arraywidth+j] = worldParticleLayer[i*arraywidth+j].toTransition;
                            if (worldParticleLayer[i*arraywidth+j].active)
                            {
                                if (worldParticleLayer[i*arraywidth+j].toTransition != null)
                                    worldParticleLayer[i*arraywidth+j] = worldParticleLayer[i*arraywidth+j].toTransition;
                                worldParticleLayer[i*arraywidth+j].update(this);
                            }
                            else
                            {
                                worldParticleLayer[i*arraywidth+j].reactivate(this);
                            }
                        }*/
                    }
                }
            }
        }
        public static void swap(uint x1, uint y1, uint x2, uint y2)
        {
            if (
                x1 * arraywidth + y1 < worldParticleLayer.Length &&
                x2 * arraywidth + y2 < worldParticleLayer.Length)
            {
                //swap positions
                /*
                if (worldParticleLayer[x1, y1) != null)
                {
                    worldParticleLayer[x1, y1).setPosition(x2, y2);
                }
                if(worldParticleLayer[x2, y2) != null)
                {
                    worldParticleLayer[x2, y2).setPosition(x1, y1);
                }
                */
                //swap objects in the array
                swapped = worldParticleLayer[x1 * arraywidth + y1];
                worldParticleLayer[x1 * arraywidth + y1] = worldParticleLayer[x2 * arraywidth + y2];
                worldParticleLayer[x2 * arraywidth + y2] = swapped;
            }
        }
        public static void draw(Microsoft.Xna.Framework.Graphics.SpriteBatch s)
        {
            Width = (uint)width / 2 + view.x;
            Height = (uint)height / 2 + view.y;
            if (backRender == null)
                backRender = new uint[width * height / 4];
            for (i = Width; i > view.x; i--)
                for (j = Height; j > view.y; j--)
                {
                    target = worldParticleLayer[i * arraywidth + j];

                    byte light = (byte)(255 - target.L);
                    if ((uint)(backRender.Length - ((j - view.y) * width / 2 - i + view.x)) < backRender.Length)
                    {
                        backRender[(uint)(backRender.Length - ((j - view.y) * width / 2 - i + view.x))] = (uint)(
                (target.A << 24) |
                ((target.B - light > 0 ? target.B - light : 0) << 16) |
                ((target.G - light > 0 ? target.G - light : 0) << 8) |
                 (target.R - light > 0 ? target.R - light : 0));
                    }
                }
            if (Render == null)
                Render = new Texture2D(s.GraphicsDevice, (int)width / 2, (int)height / 2, false, SurfaceFormat.Color);
            Render.SetData<uint>(backRender);
            s.Draw(Render, rect, Color.White);
            System.GC.Collect();
        }
    }
}


/*target = worldParticleLayer[i * arraywidth + j];
if (target.myType != 0 && target.L > 10)
{
    rect.X = (int)(i * (uint)view.multiply - factX);
    rect.Y = (int)(factY - (uint)view.multiply * (j + 1));
    s.Draw(NewParticle.myImage, rect, newParticle.getColor(target));
}*/